﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.EmpbasicService;
using CommunitySystem.ZhaoLuService;

namespace CommunitySystem
{
    public partial class ZhaoLuWindow : ChildWindow
    {
        private ZhaoLuService.ZhaoLuServiceClient Client = new ZhaoLuServiceClient();
        ZhaoLuZhiWeiModel model = new ZhaoLuZhiWeiModel();
        private EmpbasicService.EmpBasicServiceClient Client2 = new EmpbasicService.EmpBasicServiceClient();
        public int ID;
        //public int OrgID;
        public List<string> orgids = new List<string>();
        public bool IsNew = false;
        public int ZhaoLuJiHuaID;
        public string IsLocked;
        public ZhaoLuWindow(List<string> orgID, bool isnew, int iD, string chakan,string islocked,int zhaolujihuaid)
        {
            InitializeComponent();
            orgids = orgID;
            ID = iD;
            IsNew = isnew;
            ZhaoLuJiHuaID = zhaolujihuaid;
            IsLocked = islocked;
            Client.GetZhaoLuZhiWeiCompleted += new EventHandler<GetZhaoLuZhiWeiCompletedEventArgs>(Client1_GetZhaoLuZhiWeiCompleted);
            orgid.OnSelectionChanged += new OnSelectionChangedEvent(orgid_OnSelectionChanged);
            Client2.GetBianZhiModelCompleted += new EventHandler<EmpbasicService.GetBianZhiModelCompletedEventArgs>(Client_GetBianZhiModelCompleted);
            Client2.GetBianZhi_JiBenModelCompleted += new EventHandler<GetBianZhi_JiBenModelCompletedEventArgs>(Client2_GetBianZhi_JiBenModelCompleted);
            tiaojian.SelectionChanged += new SelectionChangedEventHandler(tiaojian_SelectionChanged);
            if (orgids.Count >0 && orgids[0] == "0")
            {
                tiaojian.Visibility = System.Windows.Visibility.Collapsed;
                orgid.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                tiaojian.Visibility = System.Windows.Visibility.Visible;
                orgid.Visibility = System.Windows.Visibility.Collapsed;
                Client2.GetBianZhiModelAsync(orgids[0].ToString(), 0);
                //Client.GetOrgNamebyOrgIDCompleted += new EventHandler<PublicOfficialSelectionService.GetOrgNamebyOrgIDCompletedEventArgs>(Client_GetOrgNamebyOrgIDCompleted);
                //Client.GetOrgNamebyOrgIDAsync(OrgID);
                string ord = "";
                foreach (string i in orgids)
                {
                    ord = ord + i + ",";
                }
                Client2.GetOrgForHRModelbyIDsCompleted += new EventHandler<EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs>(Client2_GetOrgForHRModelbyIDsCompleted);
                Client2.GetOrgForHRModelbyIDsAsync("(" + ord.Substring(0, ord.Length - 1) + ")");
            }
            if (IsNew)
            {
                this.Title = "新增职位信息";
                model = new ZhaoLuZhiWeiModel();
                model.OrgID = Convert.ToInt32(orgids[0]);
                orgid.Field = "OrgID";
                model.IsLocked = false;
                model.Year = ZhaoLuJiHuaID;
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑职位信息";
                if (orgids[0] != "0")
                {
                    model.OrgID = Convert.ToInt32(orgids[0]);
                    orgid.Field = "OrgID";
                }
                Client.GetZhaoLuZhiWeiAsync("0", ID, 0, 20, -1, DateTime.Today.Year, islocked,1);
            }
            if (chakan == "查看")
            {
                tiaojian.IsEnabled =
                    orgid.IsEnabled =
                    zhiwei.IsEnabled = renshu.IsEnabled = zhiweijianjie.IsEnabled = zhuanyebenke.IsEnabled =
                                                                                    zhuanyeshuoshi.IsEnabled =

                                                                                    xueli.IsEnabled =
                                                                                    xuewei.IsEnabled = zhengzhi.IsEnabled =
                                                                                    zhuanye.IsEnabled =Other.IsEnabled=

                                                                                    beizhu.IsEnabled =
                                                                                    zhaokaoduixiang.IsEnabled =
                                                                                    jicenggongzuo.IsEnabled = false;
                OKButton.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                tiaojian.IsEnabled =
                    orgid.IsEnabled =
                    zhiwei.IsEnabled = renshu.IsEnabled = zhiweijianjie.IsEnabled = zhuanyebenke.IsEnabled =
                                                                                    zhuanyeshuoshi.IsEnabled = Other.IsEnabled =

                                                                                    xueli.IsEnabled =
                                                                                    xuewei.IsEnabled = zhengzhi.IsEnabled =
                                                                                    zhuanye.IsEnabled =

                                                                                    beizhu.IsEnabled = 
                                                                                    zhaokaoduixiang.IsEnabled =
                                                                                    jicenggongzuo.IsEnabled =true;
                OKButton.Visibility = System.Windows.Visibility.Visible;
            }

        }

        void tiaojian_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Client2.GetBianZhiModelAsync(((CommunitySystem.EmpbasicService.OrgForHRModel)(((ComboBox)sender).SelectedValue)).ID.ToString(), 0);
        }

        
        void Client_GetBianZhiModelCompleted(object sender, EmpbasicService.GetBianZhiModelCompletedEventArgs e)
        {
            biaoti.ItemsSource = null;
            biaoti.ItemsSource = e.Result;
            if (biaoti.ItemsSource != null)
            {
                for (int i = 0; i < ((ObservableCollection<BianZhiModel>)biaoti.ItemsSource).Count; i++)
                {
                    if (((ObservableCollection<BianZhiModel>)biaoti.ItemsSource)[i].JiGouMingCheng == model.Department_Duty)
                        biaoti.SelectedIndex = i;
                }
            }
        }
        void orgid_OnSelectionChanged(object sender, object NewValue, object OldValue)
        {
            if (NewValue != null)
            {
                model.OrgID = Convert.ToInt32(NewValue.ToString());
                Client2.GetBianZhiModelAsync(NewValue.ToString(), 0);
                //if(!string.IsNullOrEmpty(model.DutyAttribute) && !string.IsNullOrEmpty(model.DutyLevel))
                //     Client2.GetBianZhiDetailModelAsync(model.OrgID.ToString());
            }
        }

        void gangwei_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (((ComboBox)sender).SelectedItem != null)
            {
                model.Department_Duty = ((BianZhiModel)((((ComboBox)sender).SelectedItem))).JiGouMingCheng;
            }

        }
        

        void Client2_GetOrgForHRModelbyIDsCompleted(object sender, EmpbasicService.GetOrgForHRModelbyIDsCompletedEventArgs e)
        {
            tiaojian.ItemsSource = e.Result;
            for (int i = 0; i < tiaojian.Items.Count; i++)
            {
                if (((OrgForHRModel)(tiaojian.Items[i])).ID
                    == model.OrgID)
                {
                    tiaojian.SelectedIndex = i;
                }
            }
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

            Match m = Regex.Match(((TextBox)sender).Text, pattern);   // 匹配正则表达式
            if (!string.IsNullOrEmpty(((TextBox)sender).Text) && ((TextBox)sender).Text != "0")
                if (!m.Success)   // 输入的不是数字
                {
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                    ((TextBox)sender).Text = "";
                    //((TextBox)sender).Text = param1;   // textBox内容不变

                    // 将光标定位到文本框的最后
                    //((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
                }

        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (model.OrgID == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择用人部门！");
                return;
            }
            if (tiaojian.Visibility == System.Windows.Visibility.Visible) model.OrgID = Convert.ToInt32(((CommunitySystem.EmpbasicService.OrgForHRModel)(tiaojian.SelectedItem)).ID);
            model.Department = "徐汇区";
            Client.GetZhaoLuZhiWeiAsync(model.OrgID.ToString(), 0, 0, 20, -1, ZhaoLuJiHuaID, IsLocked,0);
            
            
        }

        public int zongshu = 0;
        public List<int> zhiweiids = new List<int>();
        void Client1_GetZhaoLuZhiWeiCompleted(object sender, GetZhaoLuZhiWeiCompletedEventArgs e)
        {
            if (Convert.ToInt32(e.UserState) == 1)
            {
                if (e.Result != null)
                    model = e.Result[0];
                this.DataContext = model;
            }
            if (Convert.ToInt32(e.UserState) == 0)
            {
                if (e.Result != null && e.Result.Count > 0)
                {
                    foreach (ZhaoLuZhiWeiModel i in e.Result)
                    {
                        zongshu = zongshu + i.PeopleCount;
                    }
                }
                Client2.GetBianZhi_JiBenModelAsync(model.OrgID.ToString());
            }
        }

        void Client2_GetBianZhi_JiBenModelCompleted(object sender, GetBianZhi_JiBenModelCompletedEventArgs e)
        {
            if(e.Result != null && e.Result.Count > 0)
            {
                if (model.PeopleCount + zongshu +
                    (!string.IsNullOrEmpty(e.Result[0].JiGuanXingZheng)
                         ? Convert.ToInt32(e.Result[0].JiGuanXingZheng)
                         : 0)
                    +
                    (!string.IsNullOrEmpty(e.Result[0].ZhengFaXingZheng)
                         ? Convert.ToInt32(e.Result[0].ZhengFaXingZheng)
                         : 0)
                    + (!string.IsNullOrEmpty(e.Result[0].canzhaocount) ? Convert.ToInt32(e.Result[0].canzhaocount) : 0) >
                    (!string.IsNullOrEmpty(e.Result[0].zongcount) ? Convert.ToInt32(e.Result[0].zongcount) : 0))
                {
                    if (MessageBox.Show("单位编制已满！", "提示", MessageBoxButton.OK) == MessageBoxResult.OK)
                    {
                        model.Memo = model.Memo + "  该单位编制已满";
                    }

                }
            }
            if (IsNew == true)
            {
                //model.Year = DateTime.Today.Year;
                Client.AddZhaoLuZhiWeiCompleted += new EventHandler<AddZhaoLuZhiWeiCompletedEventArgs>(Client_AddZhaoLuZhiWeiCompleted);
                Client.AddZhaoLuZhiWeiAsync(model);
            }
            else
            {
                Client.UpdateZhaoLuZhiWeiCompleted += new EventHandler<UpdateZhaoLuZhiWeiCompletedEventArgs>(Client_UpdateZhaoLuZhiWeiCompleted);
                Client.UpdateZhaoLuZhiWeiAsync(model);
            }
            
            
            
        }

        void Client_UpdateZhaoLuZhiWeiCompleted(object sender, UpdateZhaoLuZhiWeiCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                this.DialogResult = true;
            }
        }

        void Client_AddZhaoLuZhiWeiCompleted(object sender, AddZhaoLuZhiWeiCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                this.DialogResult = true;
            }
        }
    }
}
